TARGET_NAME = ZINO_RC_PRO
TARGET_BOARD = ZINO_RC
SERIAL_PROT = COM9
MCPU = AT32F415RCT7
BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 0
SUB_VERSION := 0
BUILD_NUMBER := 1
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)_$(BUILD_DATE)
TARGET_ELF = ./build/$(TARGET_NAME)_$(BUILD_VERSION).elf
TARGET_BIN = ./build/$(TARGET_NAME)_$(BUILD_VERSION).bin
ROOT     		:= $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
targets: $(m_deps) $(TARGET_BIN) $(TARGET_ELF) $(nOBJS) 

include ./Debug/makefile

-include ./Debug/sources.mk
submks= $(addprefix $(ROOT)/Debug/, $(SUBDIRS))
submks2= $(addsuffix /subdir.mk, $(submks))
# $(info submks: $(submks2))
-include $(submks2)

c_prefix=../%
s_prefix=../%
d_prefix=./%
o_prefix=./%
nc_prefix=./%
ns_prefix=./%
nd_prefix=./build/%
no_prefix=./build/%

nC_SRCS =$(patsubst $(c_prefix),$(nc_prefix),$(C_SRCS))
nC_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(C_DEPS))
nOBJS =$(patsubst $(o_prefix),$(no_prefix),$(OBJS))
nS_SRCS =$(patsubst $(s_prefix),$(ns_prefix),$(S_SRCS))
nS_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(S_DEPS))
nS_UPPER_SRCS =$(patsubst $(s_prefix),$(ns_prefix),$(S_UPPER_SRCS))
nS_UPPER_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(S_UPPER_DEPS))
include ./ZINO/zino_source.mk
# $(info nC_DEPS: $(nC_DEPS))
# include ./applications/ZINO_FC_V4.0/zino_fc.mk


# NOTE 所有头文件路径！
c_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections \
	-Wall  -g -gdwarf-2 -DSOC_FAMILY_AT32 \
	-DSOC_SERIES_AT32F415 -DUSE_STDPERIPH_DRIVER -DAT32F415RCT7 \
	-lm -DARM_MATH_CM4 \
	-DDEV_MVID=$(MAIN_VERSION) -DDEV_SVID=$(SUB_VERSION) -DDEV_BVID=$(BUILD_NUMBER) \
	-I"$(ROOT)\drivers" \
	-I"$(ROOT)\drivers\common\include" \
	-I"$(ROOT)\drivers\f415\include" \
	-I"$(ROOT)\drivers\usbfs_library\inc" \
	-I"$(ROOT)\libraries\f415\cmsis\cm4\core_support" \
	-I"$(ROOT)\libraries\f415\cmsis\cm4\device_support" \
	-I"$(ROOT)\libraries\f415\drivers\inc" \
	-I"$(ROOT)" \
	-I"$(ROOT)\applications" \
	-I"$(ROOT)" \
	-I"$(ROOT)\rt-thread\components\drivers\include" \
	-I"$(ROOT)\rt-thread\components\drivers\include\drivers" \
	-I"$(ROOT)\rt-thread\components\drivers\sensors" \
	-I"$(ROOT)\rt-thread\components\drivers\spi" \
	-I"$(ROOT)\rt-thread\components\drivers\usb\usbdevice" \
	-I"$(ROOT)\rt-thread\components\finsh" \
	-I"$(ROOT)\rt-thread\components\libc\compilers\common\include" \
	-I"$(ROOT)\rt-thread\components\libc\compilers\newlib" \
	-I"$(ROOT)\rt-thread\components\libc\posix\io\poll" \
	-I"$(ROOT)\rt-thread\components\libc\posix\io\stdio" \
	-I"$(ROOT)\rt-thread\components\libc\posix\ipc" \
	-I"$(ROOT)\rt-thread\include" \
	-I"$(ROOT)\rt-thread\libcpu\arm\common" \
	-I"$(ROOT)\rt-thread\libcpu\arm\cortex-m4" \
	-I"$(ROOT)\rt-thread\components\drivers\hwcrypto" \
	$(inc_dir) \
	-include"$(ROOT)/rtconfig_preinc.h" \
	-std=gnu11 -MMD -MP -MD -MF "$(@:%.o=%.d)" -MT"$(@)" 

math_lib = libarm_cortexM4l_math
math_lib_dir = $(ROOT)/libraries/cmsis/lib/gcc/
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")
# NOTE -lc -lm 链接数学库时需要！!！最后！！！再链接，注意顺序
asm_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 \
	-Os -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp \
	-I"$(ROOT)" \
	-Xassembler -mimplicit-it=thumb -MMD -MP 

./build/%.o: %.c
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $< -c -g -o $@ $(c_flags)

# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp -I"D:\RT-ThreadStudio\workspace\ZINO_FC_V4" -Xassembler -mimplicit-it=thumb -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.s
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.S
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"

m_ldflags= \
	-mcpu=cortex-m4 \
	-mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections \
	-Wall  -g -gdwarf-2 \
	-T "$(ROOT)\linkscripts\AT32F415RC\link.lds" \
	-Xlinker --gc-sections -Wl,-Map,"./build/$(TARGET_NAME).map" \
	-Xlinker --cref -Wl,--print-memory-usage 
	
# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 
# -T "D:\RT-ThreadStudio\workspace\ZINO_FC_V4\linkscripts\AT32F413RC\link.lds"
#  -Xlinker --gc-sections -Wl,-Map,"rtthread.map" 
# -Xlinker --cref -o "rtthread.elf" $(OBJS) $(USER_OBJS) $(LIBS)
$(TARGET_ELF): $(nOBJS)
	@ echo "$(DATE_TIME) Linking everyting together... $@"
	@ arm-none-eabi-gcc $(m_ldflags) -o $(TARGET_ELF) $(nOBJS) -lc -lm
	@ arm-none-eabi-size $(TARGET_ELF)
	
cl:
	-$(RM) $(nOBJS) $(TARGET_ELF) $(nC_DEPS) ./build
	-@echo ' '
$(TARGET_BIN): $(TARGET_ELF)
	@ echo "$(DATE_TIME) Create binary file... $@"
	@ arm-none-eabi-objcopy -O binary $(TARGET_ELF) $(TARGET_BIN)
IAP_flash_$(TARGET): $(TARGET_BIN)
	chcp 65001
	./UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --board $(TARGET_BOARD) --mcu $(MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN)

dl: IAP_flash_$(TARGET)

